/*
 *  Queen Mary University of London MOM System
 *  
 *  Copyright Queen Mary University of London
 *  
 *  Authors:
 *  Bob Chew (bob.chew@elec.qmul.ac.uk)
 *  Beatriz Viñal Murciano (beavimu@gmail.com)
 *  John Bigham (john.bigham@elec.qmul.ac.uk)
 *  
 *  
 *  Portions of this software uses Apache Qpid
 *    http://qpid.apache.org
 *  
 *  Apache Qpid license:
 * --------------------------------------------------------------
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 * -------------------------------------------------------------
 * 
 */
package users;

import java.util.HashMap;

import javax.jms.JMSException;
import javax.jms.TopicSubscriber;

import org.apache.qpid.client.AMQTopic;

/**
 * Data needed by a subscriber to connect with a Qpid broker and interact
 * with it.
 */
public class SubscriberConnection extends UserConnection {

    /**
     * List of the subscriptions of the subscriber in the broker mapped
     * to their subscribers.
     */
    private HashMap<Subscription, TopicSubscriber> subscriptions
            = new HashMap<Subscription, TopicSubscriber>();

    /**
     * Getter for the variable 'subscriptions'.
     * @return variable 'subscriptions'
     */
    public HashMap<Subscription, TopicSubscriber> getSubscriptions() {
        return subscriptions;
    }


    /**
     * Constructor.
     *
     * @param subscriber  to whom the connection belongs
     * @param broker  to connect to
     * @param port  in which the broker is listening
     * @param exchange  to use in the broker
     */
    public SubscriberConnection(final Subscriber subscriber,
            final String broker, final int port, final String exchange) {
        super(subscriber, broker, port, exchange);
    }

    /**
     * Constructor using the default port.
     *
     * @param subscriber  to whom the connection belongs
     * @param broker  to connect to
     * @param exchange  to use in the broker
     */
    public SubscriberConnection(final Subscriber subscriber,
            final String broker, final String exchange) {
        super(subscriber, broker, exchange);
    }

    /**
     * Create a subscriber for a subscription and start listening to its
     * messages.
     *
     * Put the subscription and the subscriber in the list of subscriptions.
     *
     * @param sub  subscription to subscribe to
     * @throws JMSException  if there is any problem
     */
    protected void addSubscription(final Subscription sub)
            throws JMSException {

        //create the topic URL
        String topicURL = "topic://" + getExchange() + "/?routingkey='"
                + sub.getTopic() + "'&exclusive='false'&autodelete='true'";
        //create the topic
        AMQTopic topic = (AMQTopic) getSession().createTopic(topicURL);
        //create a subscriber
        TopicSubscriber subscriber = getSession().createDurableSubscriber(
                topic, sub.getId(), sub.getMsgSelector(), false);
//            System.out.println(getUser().getClientId()
//                    + " subscribed to topic "
//                    + subscriber.getTopic().getTopicName() + " in broker "
//                    + getBroker() + " at port " + getPort());
        //set the subscriber to listen to messages
        subscriber.setMessageListener(((Subscriber) getUser()).getListener());

        //add to the list
        getSubscriptions().put(sub, subscriber);

    }

    /**
     * Unsubscribe the subscriber from the topic of the given name.
     *
     * @param sub  subscription to unsubscribe from
     * @throws JMSException  if there is any problem
     */
    public void removeSubscription(final Subscription sub)
            throws JMSException {
        String subscriptionId = sub.getId();

        //unsubscribe
        getSession().unsubscribe(subscriptionId);
        //remove from the list
        getSubscriptions().remove(sub);
    }

}
